/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.test;

import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.isfd.constant.enums.IsfdSectionTypeEnum;
import com.comac.ins.isfd.domain.bo.*;
import com.comac.ins.isfd.domain.bo.query.IsfdCompositeMaterialQueryBo;
import com.comac.ins.isfd.domain.bo.query.IsfdLayupMetalCaseMetaQueryBo;
import com.comac.ins.isfd.domain.bo.query.IsfdMetalMaterialQueryBo;
import com.comac.ins.isfd.domain.bo.query.IsfdSectionQueryBo;
import com.comac.ins.isfd.domain.vo.*;
import com.comac.ins.isfd.factory.IsfdSectionFactory;
import com.comac.ins.isfd.service.IIsfdLayupSkinPropertieService;
import com.comac.ins.isfd.service.IIsfdSectionService;
import com.comac.ins.isfd.service.impl.*;
import com.comac.ins.isfd.service.impl.section.IsfdSectionMetadataImportServiceImpl;
import com.comac.ins.isfd.util.ExcelDataQualityCheckUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
public class IsfdMetadataImportTest {

    @Autowired
    private IsfdLayupSkinPropertieServiceImpl isfdLayupSkinPropertieServiceImpl;

    @Autowired
    private IIsfdLayupSkinPropertieService iIsfdLayupSkinPropertieService;

    @Autowired
    private IsfdSectionFactory isfdSectionFactory;
    @Autowired
    private IsfdSectionMetadataImportServiceImpl isfdSectionMetadataImportServiceImpl;

    @Autowired
    private IsfdCompositeMaterialServiceImpl isfdCompositeMaterialService;

    @Autowired
    private IsfdMetalMaterialServiceImpl isfdMetalMaterialService;

    @Autowired
    private IsfdLayupMetalCaseMetaServiceImpl isfdLayupMetalCaseMetaService;

    @Autowired
    private IFastenerServiceImpl fastenerServiceImpl;

    @Test
    public void test1() {
        IsfdLayupSkinPropertieBo isfdLayupSkinPropertieBo = new IsfdLayupSkinPropertieBo();
        List<IsfdLayupSkinPropertieVo> isfdLayupSkinPropertieVos = iIsfdLayupSkinPropertieService.queryList(isfdLayupSkinPropertieBo);
        File excelFile = new File("C:\\Users\\lijpt\\OneDrive\\Documents\\COMAC\\ISFD\\元数据管理\\测试数据\\铺层库测试.xlsx");
        Long count = isfdLayupSkinPropertieServiceImpl.excelImport(excelFile, "testMetadataManagementId");
        System.out.println("successCount: "+count);
    }

    @Test
    public void test2() {
        IsfdSectionQueryBo bo = new IsfdSectionQueryBo();
        bo.setType("ALL");
        bo.setPageNum(1);
        bo.setPageSize(10);
        bo.setIsAsc("descending");
        IIsfdSectionService sectionService = isfdSectionFactory.getSectionServiceByType(IsfdSectionTypeEnum.forValue(bo.getType()));
        TableDataInfo<IsfdSectionVo> isfdSectionVos = sectionService.queryPage(bo);
        File excelFile = new File("C:\\Users\\lijpt\\OneDrive\\Documents\\COMAC\\ISFD\\元数据管理\\测试数据\\型材库测试.xlsx");
        Long count = isfdSectionMetadataImportServiceImpl.excelImport(excelFile, "testMetadataManagementId");
        System.out.println("successCount: "+count);
    }

    @Test
    public void test3() {
        IsfdMetadataManagementBo bo = new IsfdMetadataManagementBo();
        bo.setMapFields("{\"金属材料::isfd_metal_material\":[{\"metadataField\":\"材料名称\",\"targetFieldId\":\"material_name\",\"targetField\":\"材料名称\",\"targetFieldType\":\"字符串\",\"targetFieldLength\":\"255\",\"needFlag\":\"1\",\"isClick\":false},{\"metadataField\":\"材料卡编号\",\"targetFieldId\":\"material_number\",\"targetField\":\"材料卡编号\",\"targetFieldType\":\"字符串\",\"targetFieldLength\":\"255\",\"needFlag\":\"1\",\"isClick\":false},{\"metadataField\":\"E(MPa)\",\"targetFieldId\":\"E\",\"targetField\":\"E(MPa)\",\"targetFieldType\":\"浮点型\",\"targetFieldLength\":\"65\",\"needFlag\":\"1\",\"isClick\":false},{\"metadataField\":\"NU\",\"targetFieldId\":\"NU\",\"targetField\":\"NU\",\"targetFieldType\":\"浮点型\",\"targetFieldLength\":\"65\",\"needFlag\":\"1\",\"isClick\":false},{\"metadataField\":\"G(MPa)\",\"targetFieldId\":\"G\",\"targetField\":\"G(MPa)\",\"targetFieldType\":\"浮点型\",\"targetFieldLength\":\"65\",\"needFlag\":\"1\",\"isClick\":false},{\"metadataField\":\"RHO\",\"targetFieldId\":\"RHO\",\"targetField\":\"RHO\",\"targetFieldType\":\"浮点型\",\"targetFieldLength\":\"65\",\"needFlag\":\"1\",\"isClick\":false},{\"metadataField\":\"Thm Cooe.\",\"targetFieldId\":\"thm_cooe\",\"targetField\":\"Thm Cooe.\",\"targetFieldType\":\"浮点型\",\"targetFieldLength\":\"65\",\"needFlag\":\"1\",\"isClick\":false}]}");

        File excelFile = new File("C:\\Users\\lijpt\\OneDrive\\Documents\\COMAC\\ISFD\\元数据管理\\测试数据\\CR929_Material_Lib_Composite.xlsm");
        Long count = isfdCompositeMaterialService.excelImport(excelFile, "testMetadataManagementId");
        System.out.println("successCount: "+count);
    }

    @Test
    public void test4() {
        IsfdMetalMaterialQueryBo bo = new IsfdMetalMaterialQueryBo();
        List<IsfdMetalMaterialVo> vos = isfdMetalMaterialService.queryList(bo);
        File excelFile = new File("C:\\Users\\lijpt\\OneDrive\\Documents\\COMAC\\ISFD\\元数据管理\\测试数据\\CR929_Material_Lib_Metal.xlsm");
        Long count = isfdMetalMaterialService.excelImport(excelFile, "testMetadataManagementId");
        System.out.println("successCount: "+count);
    }

    @Test
    public void test5() {
        IsfdLayupMetalCaseMetaQueryBo bo = new IsfdLayupMetalCaseMetaQueryBo();
        List<IsfdLayupMetalCaseMetaVo> vos = isfdLayupMetalCaseMetaService.queryList(bo);
        File excelFile = new File("C:\\Users\\lijpt\\OneDrive\\Documents\\COMAC\\ISFD\\元数据管理\\测试数据\\金属壳属性库测试.xlsx");
        Long count = isfdLayupMetalCaseMetaService.excelImport(excelFile, "testMetadataManagementId");
        System.out.println("successCount: "+count);
    }

    @Test
    public void test6() {
        List<FastenerCountObjectVo> vos =  fastenerServiceImpl.listCount();

        File excelFile = new File("C:\\Users\\lijpt\\OneDrive\\Documents\\COMAC\\ISFD\\元数据管理\\测试数据\\紧固件库测试.xlsx");
        Long count = fastenerServiceImpl.excelImport(excelFile, "testMetadataManagementId");
        System.out.println("successCount: "+count);
    }

    @Test
    public void test7() throws IOException {

        File file = new File("C:\\\\Users\\\\lijpt\\\\OneDrive\\\\Documents\\\\COMAC\\\\ISFD\\\\要素库\\\\要素库\\\\CR929_Layup_Lib.xlsm");
        FileInputStream inputStream = new FileInputStream(file);
        MultipartFile multipartFile = new MockMultipartFile(
                                                            file.getName(),       // 文件名
                                                            file.getName(),       // 原始文件名
                                                            "application/octet-stream",  // 文件类型
                                                            inputStream           // 文件内容
                                                        );
        // 创建 time_sheet_column 和 outlier_sheet_column 数据
        Map<String, List<String>> timeSheetColumn = new HashMap<>();
        timeSheetColumn.put("Sheet1", List.of("时间2", "啊啊啊", "时间1"));

        Map<String, List<String>> outlierSheetColumn = new HashMap<>();
        outlierSheetColumn.put("对应等效材料", List.of("Exx", "Eyy"));

        // 使用 ObjectMapper 将数据转为 JSON 字符串
        ObjectMapper objectMapper = new ObjectMapper();
        String timeCheckString = objectMapper.writerWithDefaultPrettyPrinter()
            .writeValueAsString(timeSheetColumn);
        String outlierCheckString = objectMapper.writerWithDefaultPrettyPrinter()
            .writeValueAsString(outlierSheetColumn);
        String report = ExcelDataQualityCheckUtils.excelDataQualityCheck(multipartFile, timeCheckString, outlierCheckString);
        System.out.println(report);
    }

    @Test
    public void main() {
        test1();
        test2();
        test3();
        test4();
        test5();
        test6();
    }


}
